package cn.chinasyq.photoquan.user.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import cn.chinasyq.photoquan.R;
import cn.chinasyq.photoquan.user.mode.UserMode;
import cn.chinasyq.photoquan.util.DialogUtil;
import cn.master.util.utils.RequestCodeUtil;
import cn.master.util.utils.ToastUtil;
import cn.master.volley.models.response.handler.ResponseHandler;


/**
 * 编辑昵称
 */
public class EditNickNameActivity extends Activity implements
        OnClickListener, ResponseHandler.OnNeedLoginListener, ResponseHandler.OnSucceedListener,
        ResponseHandler.OnFailedListener {

    @SuppressWarnings("unused")
    private static final String TAG = EditNickNameActivity.class.getName();
    private static final String TAG_NAME = "name";

    private ImageButton ibtnBack;
    private Button ibtnCommit = null;
    private EditText etContent = null;


    private Dialog waitForDialog = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_edit_singline_text);
        initComponent();
        initData();
        waitForDialog = DialogUtil.createProgressDialog(this);
    }


    /**
     * 初始化组件
     */
    private void initComponent() {
        ibtnBack = (ImageButton) findViewById(R.id.ibtn_back);
        ibtnCommit = (Button) findViewById(R.id.btn_commit);
        etContent = (EditText) findViewById(R.id.et_content);

        ibtnBack.setOnClickListener(this);
        ibtnCommit.setOnClickListener(this);
    }

    /**
     * 初始化数据
     */
    private void initData() {
        etContent.setText(UserMode.getCurrentUserInfo(this).getName());
        etContent.setHint("请输入名字");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ibtn_back:
                this.finish();
                break;
            case R.id.btn_commit:
                if (verifyData()) {
                    complete();
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void finish() {
        if (getCurrentFocus() != null) {
            ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(
                            getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
        }
        super.finish();
    }

    /**
     * 修改昵称
     */
    private void complete() {
        waitForDialog.show();
        ResponseHandler handler = new ResponseHandler(TAG_NAME);
        handler.setOnNeedLoginListener(this);
        handler.setOnSucceedListener(this);
        handler.setOnFailedListener(this);
        UserMode.updateAttr(this, handler, "name",
                etContent.getText().toString());
    }

    /**
     * 验证
     */
    private boolean verifyData() {
        String nickName = etContent.getText().toString();
        if (nickName.equals("")) {
            ToastUtil.showTips(this, R.mipmap.tips_warning,
                    getString(R.string.please_input_content));
            return false;
        }
        return true;
    }

    @Override
    public void onNeedLogin(String tag) {
        closeWaitForDialog();
        startActivity(new Intent(this, LoginActivity.class));
    }

    @Override
    public void onSucceed(String tag, boolean isCache, Object data) {
        etContent.getText();
        Intent i = getIntent();
        i.putExtra(TAG_NAME,etContent.getText());
        setResult(RESULT_OK, i);
        UserMode.getCurrentUserInfo(getApplicationContext()).setName(etContent.getText().toString());
        finish();
        closeWaitForDialog();
    }

    private void closeWaitForDialog() {
        if (waitForDialog != null && waitForDialog.isShowing()) {
            waitForDialog.dismiss();
        }
    }

    @Override
    public void onFailed(String tag, ResponseHandler.FailedStatus status, int resultCode, String msg) {
        if (msg == null) {
            switch (status) {
                case DATA_ERROR:
                    ToastUtil.show(getApplicationContext(), R.string.tip_data_error);
                    break;
                case NETWORK_ERROR:
                    ToastUtil.show(getApplicationContext(), R.string.servererrortips);
                    break;
            }
        } else {
            ToastUtil.show(getApplicationContext(), msg);
        }
        closeWaitForDialog();
    }


    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        if (arg0 == RequestCodeUtil.getInstance().obtainRequestCode(LoginActivity.class)) {
            if (arg1 == RESULT_OK) {
                complete();
            }
        }
    }
}
